﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using System.Data;

namespace Dal
{
    public class EnterpriseDal
    {
        public void ZhuCe(Enterprise enterprise)//注册
        {
            string sql = string.Format("insert into Enterprise values('{0}','{1}','{2}','{3}')",enterprise.Eno,enterprise.Epwd,enterprise.Ename,enterprise.Eintro);
            DbSqlHelper.ExecuteSql(sql);
        }
        public int Login(string eno, string epwd)//登录
        {
            string sql = "select *from Enterprise where eno='" + eno + "'and epwd='" + epwd + "'";
            return DbSqlHelper.LoginSql(sql);
        }
        public string Login(string eno)//获取ename
        {
            string str = "ename";
            string sql = "select *from Enterprise where eno='" + eno + "'";
            return DbSqlHelper.UserNameSql(sql,str);
        }
        public Enterprise GetSingle(string eno)//获得数据库中值
        {
            string sql = "select * from Enterprise where eno='" + eno + "'";
            DataSet ds = DbSqlHelper.Query(sql);
            Enterprise enterprise = new Enterprise();
            DataRow row = ds.Tables[0].Rows[0];
            enterprise.Eno = row["eno"].ToString();
            enterprise.Epwd = row["epwd"].ToString();
            enterprise.Ename = row["ename"].ToString();
            enterprise.Eintro = row["eintro"].ToString();
            return enterprise;
        }
        public void Update(Enterprise enterprise)
        {
            string sql = string.Format("update Enterprise set ename='{0}',eintro='{1}' where eno='{2}'",enterprise.Ename,enterprise.Eintro,enterprise.Eno);
            DbSqlHelper.ExecuteSql(sql);
        }
        public void PwdUpdate(Enterprise enterprise)
        {
            string sql = string.Format("update Enterprise set epwd='{0}' where eno='{1}'", enterprise.Epwd,enterprise.Eno);
            DbSqlHelper.ExecuteSql(sql);
        }
        public void Delete(string eno)
        {
            string sql = "delete from Enterprise where eno='" + eno + "'";
            DbSqlHelper.ExecuteSql(sql);
        }
        public Enterprise GetInfo(string name)
        {
            string sql = "select * from Enterprise where ename='" + name + "'";
            DataSet ds = DbSqlHelper.Query(sql);
            Enterprise enterprise = new Enterprise();
            DataRow row = ds.Tables[0].Rows[0];
            enterprise.Eintro = row["eintro"].ToString();
            enterprise.Ename = row["ename"].ToString();
            return enterprise;
        }
        public DataTable getEnterprises(string keyword)
        {
            string sql = "select * from Enterprise";
            if (!string.IsNullOrEmpty(keyword))
            {
                sql += " where eno like '%" + keyword + "%' or epwd like '%" + keyword + "%' or ename like '%" + keyword + "%' or eintro like '%" + keyword + "%'";
            }
            return DbSqlHelper.Query(sql).Tables[0];
        }
        public int enable(string eno)
        {
            string sql = "select paraval from properties where ParaNo='" + eno + "' and ParaType='EnterprisEnable'";
            DataTable dt = DbSqlHelper.Query(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string sql2 = "delete from properties where ParaNo='" + eno + "' and ParaType='EnterprisEnable'";
                return DbSqlHelper.ExecuteSql(sql2);
            }
            else
            {
                string sql3 = "insert into properties values('EnterprisEnable','" + eno + "','0')";
                return DbSqlHelper.ExecuteSql(sql3);
            }
        }
        public int getisok(string eno)
        {
            string sql = "select paraval from properties where ParaNo='" + eno + "' and ParaType='EnterprisEnable'";
            DataTable dt = DbSqlHelper.Query(sql).Tables[0];
            if (dt.Rows.Count > 0)
                return 0;
            return 1;
        }
    }
}
